/*
 *  rigidlink.cpp
 *  EPPI-0.0
 *
 *  Created by Juan Carlos Garcia Orden on 26/11/10.
 *
 */

#include "rigidlink.h"

RigidLink::RigidLink(ifstream &in, string cname, MecSystem *pSys)
:	Joint(in,cname,pSys),
	mass(0.0)
{	
	dim = parentSystem->dim;	/** Must be set always.  dim belongs to the mother class Joint */

	int i;
	char keyword[20];
	vector<string> auxsv(2);	/** Aux string vector */
	string auxs;
	
	while (in >> keyword && strcmp(keyword,"ENDJOINT") ) 
	{
		if(!strcmp(keyword,"P1"))
		{
			// keyword has the P1 label
			in >> pointNameA;	/** Reads the name of the point A */
			if(checkForSlash(pointNameA)==false)	addSystemName(pointNameA,parentSystem->name);

			in >> auxs;	// reads the P2 label
			in >> pointNameB;	/** Reads the name of the point B */
			if(checkForSlash(pointNameB)==false)	addSystemName(pointNameB,parentSystem->name);
			/** Extract the system name where the point is */

			auxsv[0] = pointNameA;
			auxsv[1] = pointNameB;
			//vComponents.push_back(new Consdis(auxsv,dim));
			vConstraints.push_back(new Consdis(auxsv,dim));
			//vConstraints.push_back(new ConsdisNewStl(auxsv,dim));
		}
		if(parentSystem->getProcNum()==parentSystem->getRankNum() ) cout << "	Joint " << name << " succesfully read \n";
		
		vPointNamesSurface.push_back(pointNameA);
		vPointNamesSurface.push_back(pointNameB);		
	}
}

// ***

RigidLink::RigidLink(ifstream &in, string cname, vector<string> & auxsv, MecSystem *pSys)
:	Joint(in,cname,pSys),
mass(0.0)
{	
	dim = parentSystem->dim;	/** Must be set always.  dim belongs to the mother class Joint */
	
	vConstraints.push_back(new Consdis(auxsv,dim));
	vPointNamesSurface.push_back(auxsv[0]);
	vPointNamesSurface.push_back(auxsv[1]);
}

// ***
/*
void RigidLink::setPTPointsComp()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vComponents.size(); j++)		
	//Loop on the components
	{
		auxs = (vComponents[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
		// Loop on the names of the points within each component
		{
			/ If the point it is not at the local process ...
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				// ...  create it at root system, copying the original point from the remote process
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				// ...  and add the name of the shared point at the remote process
//				cout << "Nombre del sistema = " << parentSystem->name << endl;
//				cout << "en el proceso = " << parentSystem->getProcNum() << endl;
//				cout << "Nombre del punto = " << auxs[k] << endl;
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vComponents[j]->setMPoints(maux);
		maux.clear();
	}
}

// ***

void RigidLink::setPTPointsLoad()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vLoads.size(); j++)		
	//Loop on the Loads
	{
		auxs = (vLoads[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
		// Loop on the names of the points within each component
		{
			// If the point it is not at the local process ...
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				// ...  create it at root system, copying the original point from the remote process
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				// ...  and add the name of the shared point at the remote process
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vLoads[j]->setMPoints(maux);
		maux.clear();
	}
}
*/
// ***

void RigidLink::setPTPointsCons()
{
	int j, k;
	vector<string> auxs;
	map<string, Point*, less<string> > maux;
	pair<string,Point*> mauxEntry;
	pair<string,int> sharedEntry;
	
	for(j=0; j<vConstraints.size(); j++)		
		//Loop on the Loads
	{
		auxs = (vConstraints[j]->getPointNames());
		for(k=0; k<auxs.size(); k++)		
			// Loop on the names of the points within each component
		{
			// If the point it is not at the local process ...
			if( (parentSystem->checkIfPointProcess(auxs[k]))==false )
			{
				// ...  create it at root system, copying the original point from the remote process
				parentSystem->createPointAtRoot( parentSystem->getPTPoint(auxs[k]) );
				// ...  and add the name of the shared point at the remote process
				parentSystem->addRemoteSharedPoint(auxs[k],parentSystem->getProcNum());
			}
			mauxEntry.first = auxs[k];
			mauxEntry.second = (parentSystem->getPTPointLocal(auxs[k]));
			maux.insert(mauxEntry);
		}
		vConstraints[j]->setMPoints(maux);
		maux.clear();
	}	
}

// ***
/*
void RigidLink::setPTBodies()
{}
*/
// ***

void RigidLink::setPPointSurface()
{
	// Build the vector of pointers to the surface points
	vPPointSurface.resize(vPointNamesSurface.size());
	
	int i;
	string auxs;
	for(i=0;i<vPointNamesSurface.size();i++)
	{
		auxs = vPointNamesSurface[i];
		if(checkForSlash(auxs)==false)	addSystemName(auxs,parentSystem->name);
		vPPointSurface[i] = parentSystem->getPTPointLocal( auxs );
	}	
}

// ***

void RigidLink::writeMSH(ofstream &fGraph)
{}

// ***

void RigidLink::writeVTU(ofstream &fGraph)
{
	int i;
	int j;
	int nt;
	
	localFGraph << "<?xml version=\"1.0\"?>\n";
	localFGraph << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
	localFGraph << "	<UnstructuredGrid>\n";
	localFGraph << "		<Piece NumberOfPoints=\"" << 2 << "\" NumberOfCells=\"" << 1 << "\">\n";
	localFGraph << "			<Points>\n";
	localFGraph << "				<DataArray type=\"Float64\" NumberOfComponents=\"3\" format=\"ascii\">";
	
	for(i=0; i<2; ++i)
	{
		localFGraph << "\n";
		localFGraph << "				";
		nt = vPPointSurface[i]->globalTNum.size();	// Number of translational dofs
		for (j=0; j<nt; ++j)
		{
			localFGraph << parentSystem->getDof( vPPointSurface[i]->globalTNum[j] ) << "\t"; 
		}
		if (nt<2 && nt!=0) localFGraph << 0.0;	// Second coordinate, just in case is 1D
		if (nt<3 && nt!=0) localFGraph << "\t" << 0.0;	// Third coordinate, just in case is 2D
	}
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "			</Points>\n";
	localFGraph<< "			<Cells>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">";
	
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << 0 << "\t" << 1;
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << 2;
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph << "				<DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">";
	localFGraph << "\n";
	localFGraph << "					";
	
	localFGraph << 3;
	
	localFGraph << "\n";
	localFGraph << "				</DataArray>\n";
	localFGraph<< "			</Cells>\n";
	
	localFGraph << "		</Piece>\n";
	
	localFGraph << "	</UnstructuredGrid>\n";
	localFGraph << "</VTKFile>\n";	
}

// ***

void RigidLink::writeOOGL(ofstream &fGraph)
{}

// ***
/*
void RigidLink::writeFileTemp(const double&, ofstream &)
{}
*/
// ***
/*
void RigidLink::setIniPosJoint(vector<double>& iniValues)
{}
*/